package com.snk.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.snk.common.pojo.PageObject;
import com.snk.mapper.CartMapper;
import com.snk.mapper.ItemMapper;
import com.snk.pojo.Cart;
import com.snk.pojo.Item;
import com.snk.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
   @Autowired
   private CartMapper cartMapper;
   @Autowired
   private ItemMapper itemMapper;

    @Override
    public PageObject<Cart> findCartList(long userId) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Cart> cartList=cartMapper.selectList(queryWrapper);
        PageObject<Cart> PageObject = new PageObject<Cart>(10L,cartList,10,1L);
        return PageObject;
    }

    @Override
    @Transactional
    public void saveCart(Cart cart) {
        QueryWrapper<Item> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("id", cart.getItemId());
        Item item=itemMapper.selectOne(queryWrapper1);
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cart.getUserId());
        queryWrapper.eq("item_id", cart.getItemId());
        Cart cartDB = cartMapper.selectOne(queryWrapper);
        cart.setItemTitle(item.getTitle()).setItemImage(item.getImage()).setItemPrice(item.getPrice()).setNum(1);
        System.out.println(cart);
        if(cartDB == null){ //表示数据库中没有记录,直接入库
            System.out.println("入库");
            cartMapper.insert(cart);
            System.out.println("入库成功");
        }else{  //数据库中有记录 只改数量
            System.out.println("增加数量");
            int num = cart.getNum() + cartDB.getNum();
            //1.手写sql 2.简洁封装
            Cart cartTemp = new Cart();
            cartTemp.setId(cartDB.getId()).setNum(num);
            System.out.println("num="+num);
            cartMapper.updateById(cartTemp);
            System.out.println("增加数量成功");
        }
    }

    @Override
    public void updateCart(Cart cart) {
        //查询订单是否存在
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", cart.getUserId());
        queryWrapper.eq("item_id", cart.getItemId());
        Cart cart1=cartMapper.selectOne(queryWrapper);
        if (cart1!=null){
            System.out.println("数据存在");
            int num=cart.getNum()+cart1.getNum();
            if (num>0){
                System.out.println("数量大于0更新数量");
                Cart cartTemp = new Cart();
                cartTemp.setId(cart1.getId()).setNum(num);
                cartMapper.updateById(cartTemp);
            }else {
                System.out.println("数据小于0删除");
                cartMapper.deleteById(cart1.getId());
            }
        }else {
            System.out.println("数据不存在");
            if (cart.getNum()>0) {
                System.out.println("入库");
                Item item = itemMapper.selectById(cart.getItemId());
                cart.setItemTitle(item.getTitle()).setItemImage(item.getImage()).setItemPrice(item.getPrice()).setNum(cart.getNum());
                cartMapper.insert(cart);
            }
        }
    }

}
